//
//  MyselfViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MyselfViewController.h"
#import "MyselfHeaderView.h"
#import "MyselfTableViewCell.h"
#import "InfoViewController.h"
#import "MyWishViewController.h"
#import "MyCommitViewController.h"
#import "MyCollectionViewController.h"
#import "SendSuggestViewController.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "LoginViewController.h"
#import "MyMessageViewController.h"

@interface MyselfViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)MyselfHeaderView * header;
@property (nonatomic, strong)NSArray * showStringArray;
@property (nonatomic, copy)NSURL * url;
@property (nonatomic, copy)NSString * name;
@property (nonatomic, copy)NSString * person;

@end

@implementation MyselfViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSString * token = DEF_GET_OBJECT(@"token");
    
    NSDictionary * dict = @{@"token":token};
    
    [[NetworkTool sharedTools] POST:@"/index/Member/info" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            NSDictionary * dict = responseObject[@"data"];
            
            self.header.name = dict[@"username"];
            self.name = dict[@"username"];
            NSURL * url = [NSURL URLWithString:dict[@"headimg"]];
            self.header.imageUrl = url;
            self.url = url;
            self.header.experson = dict[@"signature"];
            self.person = dict[@"signature"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    self.showStringArray = @[@"我的消息",@"意见反馈",@"退出登录",@"注册/登录"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickMyselfHeaderView:) name:@"clickMyselfHeaderView" object:nil];
    
    [self setupUI];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 45 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyselfTableViewCell class] forCellReuseIdentifier:@"MyselfListCell"];
    [self.tableView registerClass:[MyselfHeaderView class] forHeaderFooterViewReuseIdentifier:@"MyselfHeaderView"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyselfTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyselfListCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = self.showStringArray[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 148 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    self.header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyselfHeaderView"];
    
    return self.header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    if (indexPath.row == 1) {
        
        SendSuggestViewController * suggestVC = [SendSuggestViewController new];
        
        [self.navigationController pushViewController:suggestVC animated:YES];
    }else if (indexPath.row == 2){
        
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"是否确定要注销登录"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
                                                                  
                                                                  NSDictionary *dictionary = [defatluts dictionaryRepresentation];
                                                                  
                                                                  for(NSString *key in [dictionary allKeys]){
                                                                      
                                                                      [defatluts removeObjectForKey:key];
                                                                      [defatluts synchronize];
                                                                  }
                                                                  
                                                                  
                                                                  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                                                                  
                                                                  MainViewController * navVC = [[MainViewController alloc] initWithRootViewController:[LoginViewController new]];
                                                                  
                                                                  appDelegate.window.rootViewController = navVC;
                                                              }];
        UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  //                                                                          NSLog(@"action = %@", action);
                                                              }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancleAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else if (indexPath.row == 3){

        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
        MainViewController * navVC = [[MainViewController alloc] initWithRootViewController:[LoginViewController new]];
        
        appDelegate.window.rootViewController = navVC;
    }else if (indexPath.row == 0){
        
        MyMessageViewController * msgVC = [MyMessageViewController new];
        
        [self.navigationController pushViewController:msgVC animated:YES];
    }
    
    
    SHOW_TAB;
}


- (void)clickMyselfHeaderView:(NSNotification *)noti{
    
    NSString * type = noti.userInfo[@"type"];
    
    HIDDEN_TAB;
    if ([type isEqualToString:@"info"]) {
        
        InfoViewController * infoVC = [InfoViewController new];
        
        infoVC.name = self.name;
        infoVC.person = self.person;
        infoVC.url = self.url;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }else if ([type isEqualToString:@"wish"]){
        
        MyWishViewController * wishVC = [MyWishViewController new];
        
        [self.navigationController pushViewController:wishVC animated:YES];
    }else if ([type isEqualToString:@"commit"]){
        
        MyCommitViewController * commitVC = [MyCommitViewController new];
        
        [self.navigationController pushViewController:commitVC animated:YES];
    }else if ([type isEqualToString:@"collection"]){
        
        MyCollectionViewController * collectVC = [MyCollectionViewController new];
        
        [self.navigationController pushViewController:collectVC animated:YES];
    }
    
    
    SHOW_TAB;
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
